import axios from 'axios'

// 配置基础URL
const API_BASE_URL = 'http://localhost:8081'

// 创建axios实例
const api = axios.create({
  baseURL: API_BASE_URL,
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
})

// 请求拦截器
api.interceptors.request.use(
  (config) => {
    const token = localStorage.getItem('token')
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

// 响应拦截器
api.interceptors.response.use(
  (response) => {
    return response
  },
  (error) => {
    if (error.response?.status === 401) {
      localStorage.removeItem('token')
      window.location.href = '/login'
    }
    return Promise.reject(error)
  }
)

// 练习管理API
export const exerciseAPI = {
  // 创建练习
  createExercise: (exerciseData) => api.post('/api/exercises', exerciseData),
  
  // 获取练习信息
  getExercise: (exerciseId) => api.get(`/api/exercises/${exerciseId}`),
  
  // 获取练习详情
  getExerciseDetail: (exerciseId) => api.get(`/api/exercises/${exerciseId}/detail`),
  
  // 获取练习题目
  getExerciseQuestions: (exerciseId) => api.get(`/api/exercises/${exerciseId}/questions`),
  
  // 获取所有练习
  getAllExercises: () => api.get('/api/exercises'),
  
  // 更新练习信息
  updateExercise: (exerciseId, exerciseData) => api.put(`/api/exercises/${exerciseId}`, exerciseData),
  
  // 删除练习
  deleteExercise: (exerciseId) => api.delete(`/api/exercises/${exerciseId}`),
  
  // 级联删除练习
  cascadeDeleteExercise: (exerciseId) => api.post(`/api/exercises/cascade/${exerciseId}`),
  
  // 获取课程练习
  getExercisesByCourse: (courseId) => api.get(`/api/exercises/course/${courseId}/all`),
  
  // 获取班级练习
  getExercisesByClass: (classId) => api.get(`/api/exercises/class/${classId}`),
  
  // 获取用户角色
  getUserRole: () => api.get('/api/exercises/role'),
  
  // 获取收藏练习
  getFavoriteExercises: (params) => api.get('/api/exercises/favorite', { params }),
  
  // 收藏练习
  favoriteExercise: (data) => api.post('/api/exercises/favorite', null, { params: data }),
  
  // 取消收藏
  unfavoriteExercise: (data) => api.post('/api/exercises/unfavorite', null, { params: data }),
  
  // 获取错题
  getWrongExercises: (params) => api.get('/api/exercises/wrong', { params }),
  
  // 获取已完成练习
  getFinishedExercises: (params) => api.get('/api/exercises/finished', { params }),
  
  // 获取未完成练习
  getUnfinishedExercises: (params) => api.get('/api/exercises/unfinished', { params }),
  
  // 获取练习完成率
  getExerciseCompletionRate: (exerciseId) => api.get(`/api/exercises/${exerciseId}/completion-rate`),
  
  // 检查练习是否存在
  exerciseExists: (exerciseId) => api.get(`/api/exercises/${exerciseId}/exists`)
}

// 练习提交API
export const submissionAPI = {
  // 提交练习答案
  submitExercise: (submissionData) => api.post('/api/exercises/submitted', submissionData),
  
  // 获取学生练习记录
  getStudentExercises: (exerciseId) => api.get(`/api/exercises/${exerciseId}/student-exercises`),
  
  // 获取班级练习成绩
  getClassGrades: (classId) => api.get(`/api/exercises/classes/${classId}/grading`),
  
  // 获取练习排名
  getExerciseRanking: (data) => api.post('/api/exercises/ranking', data)
}

// 题库管理API
export const questionBankAPI = {
  // 加载课程题库
  loadCourseQuestions: (courseId) => api.get(`/api/exercises/question-bank/${courseId}`),
  
  // 添加题目到题库
  addQuestionToBank: (courseId, questionData) => api.post(`/api/exercises/question-bank/${courseId}/questions`, questionData),
  
  // 从题库删除题目
  deleteQuestionFromBank: (courseId, questionId) => api.delete(`/api/exercises/question-bank/${courseId}/questions/${questionId}`)
}

export default api
